package com.xiaoke.api.controller;

import com.xiaoke.api.service.CollectService;
import com.xiaoke.entity.Collect;
import com.xiaoke.entity.User;
import com.xiaoke.shiro.SecurityService;
import com.xiaoke.util.common.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private SecurityService securityService;

    @GetMapping("/addordelete")
    public AjaxMsg addOrDelete(String goodsId){
        User user = securityService.getCurrentUser();
        Collect collect = collectService.findByUserAndGoods(user.getId(),goodsId);
        if(collect != null)
            collectService.delete(collect.getId());
        else {
            collect = new Collect();
            collect.setUserId(user.getId());
            collect.setValueId(goodsId);
            collectService.insert(collect);
        }
        return new AjaxMsg();
    }

    @GetMapping("/list")
    public AjaxMsg list(){
        User user = securityService.getCurrentUser();
        List<Map<String,Object>> res = collectService.findByUser(user.getId());
        return new AjaxMsg(res);
    }
}
